﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using WebUI.Controls;

namespace WebUI.EditPages
{
    public partial class Receivable : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (!IsPostBack)
            {
                LoadUnpaid(0);
                PaymentMethod methods = PaymentMethod.GetAllMethods();
                Utilities.LoadDropDownListNoSelect(ddlPaymentMethod, methods.DefaultView, PaymentMethod.ColumnNames.Name, PaymentMethod.ColumnNames.ID);
                methods.Filter = PaymentMethod.ColumnNames.Default + "=1 and " + PaymentMethod.ColumnNames.MethodType + "='B'";
                if (methods.RowCount > 0)
                    ddlPaymentMethod.SelectedValue = methods.ID.ToString();

                Customers cust = Customers.GetAllActiveCustomers();
                Utilities.LoadDropDownList(ddlCustomer, cust.DefaultView, Customers.ColumnNames.CustomerName, Customers.ColumnNames.ID, "--All--");
            }

            btnPay.Enabled = SessionUser.BranchID == Branches.CurrentBranchID;
            btnPaymentHistory.Enabled = SessionUser.BranchID == Branches.CurrentBranchID;
        }

        public void btnSearch_OnClick(object sender, EventArgs e)
        {
            View_Invoices ivc = null;

            invoiceList.SourceObject = ivc;
 
            RefreshList();
        }

        private void RefreshList()
        {
            invoiceList.CurrentPageIndex = 0;
            invoiceList.SortExpression = View_Invoices.ColumnNames.CreatedDate;
            invoiceList.SortDirection = BaseListControl.SORT_DESC;
            
            invoiceList.Refresh();

            decimal total = 0;
            decimal paid = 0;
            invoiceList.SourceObject.Rewind();
            for (int i = 0; i < invoiceList.SourceObject.RowCount; i++)
            {
                total += (decimal)invoiceList.SourceObject.GetColumn("TotalAmount");
                paid += (decimal)invoiceList.SourceObject.GetColumn("PaidAmount");
                invoiceList.SourceObject.MoveNext();
            }

            lblTotal.Text = string.Format("{0:C}", total);
            lblPaid.Text = string.Format("{0:C}", paid);
            lblBal.Text = string.Format("{0:C}", total - paid);
        }

        protected void btnEdit_OnClick(object sender, EventArgs e)
        {
            invoiceList.ReadOnly = false;
            invoiceList.Refresh();
        }

        private void LoadUnpaid(int id)
        {
            View_Invoices ivc = null;
            ivc = View_Invoices.GetUnpaidInvoices(id);
            invoiceList.SourceObject = ivc;
            
            RefreshList();
        }

        protected void btnPay_OnClick(object sender, EventArgs e)
        {
            invoiceList.Pay(int.Parse(ddlPaymentMethod.SelectedValue));

            if(ddlCustomer.SelectedIndex <= 0)
                LoadUnpaid(0);
            else
                LoadUnpaid(int.Parse(ddlCustomer.SelectedValue));
        }

        protected void ddlCustomer_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCustomer.SelectedIndex <= 0)
                LoadUnpaid(0);
            else
                LoadUnpaid(int.Parse(ddlCustomer.SelectedValue));
        }
    }
}